<?php
namespace jy_qqdian;
defined('BY_JYA') or exit('error');
/*
	商品接口
	https://developers.weixin.qq.com/miniprogram/dev/framework/ministore/minishopopencomponent/API/spu/add_spu.html
*/
class order extends \plugin{
	//添加订单
	public function add($order){
		if($order['status']==10){
			$status=0;
		}elseif($order['status']==20){
			$status=1;
		}elseif($order['status']==30){
			$status=2;
		}elseif($order['status']==100){
			$status=3;
		}elseif($order['status']==200){
			$status=5;
		}elseif($order['status']==250){
			$status=4;
		}
		$goods=array();
		foreach($order['order_detail']['product_infos'] as $gv){
			$goods[]=array(
				'goodsid'=>$gv['product_id'],
				'optionid'=>$gv['sku_id'],
				'price'=>$gv['slae_price'],
				'total'=>$gv['sku_cnt']?:1,
			);
		}
		$address=array(
			'pcd'=>array(
				'province'=>$order['order_detail']['delivery_info']['address_info']['province_name'],
				'city'=>$order['order_detail']['delivery_info']['address_info']['city_name'],
				'district'=>$order['order_detail']['delivery_info']['address_info']['county_name'],		
			),
			'address'=>$order['order_detail']['delivery_info']['address_info']['detail_info'],
			'name'=>$order['order_detail']['delivery_info']['address_info']['username'],
			'mobile'=>$order['order_detail']['delivery_info']['address_info']['tel_number'],
		);
		$new_order=array(
			'mid'=>$_SESSION['uid'],
			'status'=>$status,
			'createtime'=>strtotime($order['create_time']),
			'goodsprice'=>$order['order_detail']['price_info']['product_price'],
			'price'=>$order['order_detail']['price_info']['order_price'],
			'dispatchprice'=>$order['order_detail']['price_info']['freight'],
			'remark'=>$order['ext_info']['customer_notes'],
			'goods'=>$goods,
			'address'=>$address,
			'paytype'=>'wechat',
			'transid'=>$order['order_detail']['pay_info']['transaction_id'],
			'ispay'=>$order['order_detail']['pay_info']['pay_time']?1:0,
			'paytime'=>$order['order_detail']['pay_info']['pay_time'],
			'plugin_name'=>'jy_qqdian'
		);
		$order_id=$this->create($new_order);
		if($status==1 || $status==2 || $status==3){
			$this->order_pay($order_id,$status,$new_order['transid']);
		}
		return $order_id;
	}
	public function create($params,$uid='',$url=''){
		$this->order=$params;
		$this->order['createtime']=TIMESTAMP;
		$uid=$uid?$uid:$_SESSION['uid'];
		$area_code=pdo_getcolumn('jy_weishop_member',array('mid'=>$params['mid']?:$uid),'area_code');
		$order=array(
			'uniacid'=>$_SESSION['uniacid'],
			'ordersn'=>$params['ordersn']?:get_ordersn($this->tablename('order'),'ordersn'),
			'mid'=>$params['mid']?:$uid,
			'price'=>$params['price'],
			'ding_price'=>$params['ding_price']?:0,
			'outnum_fee'=>$params['outnum_fee'],
			'goodsprice'=>$params['goodsprice'],
			'dispatchprice'=>$params['dispatchprice'],
			'level_de_price'=>$params['level_de_price'],
			'luck_de_price'=>$params['luck_de_price'],
			'buyxiu_de_price'=>$params['buyxiu_de_price'],
			'enough_de_price'=>$params['enough_de_price'],
			'coupon_de_price'=>$params['coupon_de_price'],
			'deductcredit'=>$params['deduct_credit'],
			'deductcredit2'=>$params['deduct_credit2'],
			'couponid'=>$params['couponid'],
			'remark'=>$params['remark'],
			'sendtype'=>$params['sendtype'],
			'addressid'=>$params['addressid'],
			'address'=>ijson_encode($params['address']),
			'storeid'=>$params['storeid'],
			'merchid'=>$params['merchid']?:$params['goods']['0']['merchid'],
			'plugin_name'=>$params['plugin_name']?:$_SESSION['current_plugin'],
			'tableid'=>intval($params['tableid'])?:$_SESSION['table_id'],
			'area_code'=>$area_code,
			'isdish'=>intval($params['isdish']),
			'diyformdata'=>$params['diyfields'],
			'createtime'=>$params['createtime']?:TIMESTAMP,
			'teamid'=>intval($params['teamid']),
			'is_team'=>intval($params['is_team']),
			'send_status'=>intval($params['send_status']),
			'out_upfee'=>$params['out_upfee'],
			'api_id'=>$params['api_id']?:$params['goods']['0']['api_id'],
			'api_pluginname'=>$params['api_pluginname']?:$params['goods']['0']['api_pluginname'],
		);
		/*
			核销
		*/
		$num=0;
		$verifynum=0;
		foreach($params['goods'] as &$gv){
			$this->model('goods')->check_buy($gv['goodsid'],$gv['total'],$gv['optionid']);
			$goodsinfo=$this->pdo_get('goods',array('id'=>$gv['goodsid']),array('merchid','totalcnf','api_id','api_pluginname'));
			$gv['merchid']=$goodsinfo['merchid'];
			$gv['api_id']=$goodsinfo['api_id'];
			$gv['api_pluginname']=$goodsinfo['api_pluginname'];
			if($goodsinfo['totalcnf']==0){
				//拍下减库存
				if($gv['optionid']){
					pdo_update('jy_weishop_goods_option',array('stock -='=>$gv['total']),array('id'=>$gv['optionid']));
				}else{
					pdo_update('jy_weishop_goods',array('total -='=>$gv['total']),array('id'=>$gv['goodsid']));
				}
			}
			//判断订单核销分量
			$num +=1;
			if($gv['isverify']==1){
				$verifynum +=1;
			}
		}
		if($verifynum==$num && $verifynum>0){
			$order['isverify']=2;
		}elseif($verifynum<$num && $verifynum>0){
			$order['isverify']=1;
		}else{
			$order['isverify']=0;
		}
		//来源追加订单
		if($params['oldorderid']){
			$uporder=$order;
			unset($uporder['mid'],$uporder['uniacid'],$uporder['ordersn'],$uporder['createtime'],$uporder['storeid']);
			$order_id=$params['oldorderid'];
			$this->pdo_update('order',$uporder,array('id'=>$order_id));
		}else{
			$this->pdo_insert('order',$order);
			$order_id=$this->pdo_insertid();
		}
		$this->order['id']=$order_id;
		$this->order['ordersn']=$order['ordersn'];
		//整理订单商品
		foreach($params['goods'] as $item){
			$new_order_goods=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$params['mid'],
				'orderid'=>$order_id,
				'goodsid'=>$item['goodsid'],
				'optionid'=>$item['optionid'],
				'price'=>$item['price'],
				'total'=>$item['total'],
				'merchid'=>$item['merchid'],
				'plugin_name'=>$this->session('fromplugin_name'),
				'frompid'=>$this->session('frompid'),
				'fee_desc'=>ijson_encode($fee_desc),
				'api_id'=>$item['api_id'],
				'api_pluginname'=>$item['api_pluginname'],
				'active'=>$item['active']?ijson_encode($item['active']):'',
				'createtime'=>TIMESTAMP,
			);
			if($new_order_goods['optionid']){
					$specs=$this->pdo_get('goods_option',array('id'=>$item['optionid']),'specs');
					$specstitle=explode("_",$specs['specs']);
					foreach($specstitle as &$spec){
						$spec=$this->pdo_getcolumn('goods_spec_item',array('id'=>$spec),'title');
					}
					$new_order_goods['optiondata']=implode(" ",$specstitle);
			}
			$new_order_goods['total_price']=$new_order_goods['price']*$new_order_goods['total'];
			$this->pdo_insert('order_goods',$new_order_goods);
			//清理购物车
			if($item['id']){
				$this->pdo_delete('member_cart',array('id'=>$item['id']));
			}
		}
		return $order_id;
	}
	//订单完成
	public function order_pay($orderid=0,$status=0,$transid=''){
		$params=array(
			'status'=>$status,
			'paytype'=>'wechat',
			'transid'=>$transid,
		);
		model('jy_weishop/order')->order_pay($orderid,$params);
		return true;
	}
}